#Region " Imports "

Imports System.Data
Imports BlazeApps.Library.Forum

#End Region

Partial Class Usercontrols_Admin_Forums
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Private DataPageLocal As New PagedDataSource

    Public Property sortCriteria() As String
        Get
            Return CStr(ViewState("sortCriteria"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortCriteria") = Value
        End Set
    End Property

    Public Property sortDir() As String
        Get
            Return CStr(ViewState("sortDir"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortDir") = Value
        End Set
    End Property

    Protected Property ForumGuid() As Guid
        Get
            Return GetGuidFromVS("ForumID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("ForumID", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Forum Management")

        If IsUserAuthenticated = True Then

            If Me.Page.IsPostBack = False Then

                'Set Default Sort Order and Direction
                Me.sortCriteria = "sortorder"
                Me.sortDir = "asc"

                LoadPage()
            End If

            ConfigBaseNavCtrls()

            If Me.Page.IsPostBack = False Then
                BindDataItems()
            End If

        End If

        Me.uxforum_forumPanel.Visible = ShowControls(SecurityMask.Admin, True)

    End Sub

#End Region

#Region " Save Data "

    Private Sub SaveForum()
        Dim Item As Forum = Forum.NewForum

        Item.ForumName = Me.uxForumNameTextBox.Text.Trim
        Item.CategoryID = GetGuidFromSelectedItem(Me.uxCategoriesDropDownList)
        Item.Description = Me.uxDescriptionTextBox.Text.Trim
        Item.SortOrder = CType(Me.uxSortOrderTextbox.Text.Trim, Integer)
        Item.LastUserID = CurrentUserID
        Item.Locked = uxLockedCheckBox.Checked
        Item.Moderated = Me.uxModeratedCheckBox.Checked
        Item.Save()

        ForumGuid = Item.ForumID

        EnableControls(True)

        BindDataItems()
    End Sub

    Private Sub UpdateForum()
        Dim Item As Forum = Forum.GetForum(ForumGuid)

        Item.ForumName = Me.uxForumNameTextBox.Text.Trim
        Item.CategoryID = GetGuidFromSelectedItem(Me.uxCategoriesDropDownList)
        Item.Description = Me.uxDescriptionTextBox.Text.Trim
        Item.SortOrder = CType(Me.uxSortOrderTextbox.Text.Trim, Integer)
        Item.LastUserID = CurrentUserID
        Item.Locked = uxLockedCheckBox.Checked
        Item.Moderated = Me.uxModeratedCheckBox.Checked
        Item.Save()

        BindDataItems()
    End Sub

#End Region

#Region " Load Data "

    Private Sub LoadPage()
        LoadPageSize(Me.uxPageSizeDropDownList)
        LoadCategoryList(Me.uxCategoriesDropDownList)
    End Sub

    Private Sub GetForum()
        Dim Item As Forum = Forum.GetForum(ForumGuid)

        Me.uxForumNameTextBox.Text = Item.ForumName
        Me.uxCategoriesDropDownList.SelectedValue = Item.CategoryID.ToString
        Me.uxDescriptionTextBox.Text = Item.Description
        Me.uxSortOrderTextbox.Text = Item.SortOrder

        LoadForumModerators()
        EnableControls(True)
    End Sub

    Public Overloads Overrides Sub BindDataItems()

        Dim List As New ForumList
        Dim DT As New DataTable
        Dim DV As New DataView

        DT = List.GetForums()

        Me.RecordCount = DT.Rows.Count

        DV.Table = DT

        DV.Sort = Me.sortCriteria & " " & Me.sortDir

        Me.DataPage.DataSource = DV
        Me.DataPage.AllowPaging = True
        Me.DataPage.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.DataPage.CurrentPageIndex = Me.PageNumber

        CreatePagingLinks()

        With Me.uxResultsGridView
            .DataSource = Me.DataPage
            .DataBind()
        End With

        Dim MU As New MembershipUtility

        With Me.uxUsersDropDownList
            .DataSource = MU.GetAllUsers()
            .DataTextField = "UserName"
            .DataValueField = "UserName"
            .DataBind()
        End With

    End Sub

    Private Sub LoadForumModerators()
        Dim List As New ForumList

        With Me.uxModeratorsDataList
            .DataSource = List.GetForumModerators(ForumGuid)
            .DataBind()
        End With
    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub DeleteForum(ByVal sender As Object, ByVal e As CommandEventArgs)
        Forum.DeleteForum(ConvertToGuid(e.CommandArgument))
        BindDataItems()
    End Sub

    Protected Sub EditForum(ByVal sender As Object, ByVal e As CommandEventArgs)
        ForumGuid = ConvertToGuid(e.CommandArgument)
        GetForum()
    End Sub

    Protected Sub uxAddModeratorButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxAddModeratorButton.Click
        Dim item As ForumModerators = ForumModerators.NewForumModerators

        item.ForumID = ForumGuid
        item.UserID = UserID(Me.uxUsersDropDownList.SelectedValue.ToString().Trim())

        item.Save()

        LoadForumModerators()

    End Sub

    Protected Sub DeleteModerator(ByVal sender As Object, ByVal e As CommandEventArgs)
        ForumModerators.DeleteForumModerators(ForumGuid, ConvertToGuid(e.CommandArgument.ToString().Trim()))

        LoadForumModerators()
    End Sub

    Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click
        If Page.IsValid Then
            If ForumGuid = Guid.Empty Then
                SaveForum()
            Else
                UpdateForum()
            End If
        End If
    End Sub

    Protected Sub uxResultsGridView_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles uxResultsGridView.Sorting
        'Get the Sort by Value from the Sortexpression field on the column
        If Me.sortCriteria = e.SortExpression Then
            If Me.sortDir = "desc" Then
                Me.sortDir = "asc"
            Else
                Me.sortDir = "desc"
            End If
        End If

        ' Assign the column clicked to the sortCriteria property
        Me.sortCriteria = e.SortExpression

        'Calls the function to rebuild the datagrid view, with the new sort order
        BindDataItems()
    End Sub

    Protected Sub SecureItem(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim Item As String() = Split(e.CommandArgument.ToString, "|")

        Me.uxSecurityModalPopupExtender.Show()

        uxSecurity.ItemGuid = ConvertToGuid(Item(0).ToString.Trim)
        uxSecurity.ItemName = Item(1).ToString.Trim
        uxSecurity.BindDataItems()
        uxSecurity.LoadItemName()

    End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub ConfigBaseNavCtrls()

        Me.PagingTopPanel = Me.uxPagingTopPanel
        Me.PagingBottomPanel = Me.uxPagingBottomPanel

        Me.RepeaterTop = Me.uxPagesTopRepeater
        Me.RepeaterBottom = Me.uxPagesBottomRepeater

        Me.NextTopLinkButton = Me.uxTopNextLinkButton
        Me.PreviousTopLinkButton = Me.uxTopPrevLinkButton
        Me.NextBottomLinkButton = Me.uxBottomNextLinkButton
        Me.PreviousBottomLinkButton = Me.uxBottomPrevLinkButton

        Me.DataPage = DataPageLocal
        Me.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.PagesToDisplay = 10

        Me.ItemsPerPageDropDown = Me.uxPageSizeDropDownList
        Me.ItemsPerPageLabel = Me.uxItemsPerPageLabel
        Me.PagerInfoLabel = Me.uxPagerInfoLabel

        AddHandler Me.RepeaterTop.ItemCommand, AddressOf PagingRepeater_ItemCommand
        AddHandler Me.RepeaterBottom.ItemCommand, AddressOf PagingRepeater_ItemCommand

    End Sub

    Private Sub EnableControls(ByVal Active As Boolean)
        Me.uxAddModeratorButton.Enabled = Active
    End Sub

#End Region

End Class
